<?php

namespace App\Admin\Controllers;

use App\Admin\Repositories\BoxPointproduct;
use Dcat\Admin\Support\JavaScript;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Illuminate\Support\Str;
use Dcat\Admin\Http\Controllers\AdminController;

class BoxPointproductController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new BoxPointproduct(), function (Grid $grid) {
            $grid->selector(function (Grid\Tools\Selector $selector) {
                $selector->select('class_id', '商品类型', [
                    1 => '数码电器',
                    2 => '文具',
                    3 => '生活用品',
                    4 => '美食饮品',
                    5 => '机械运动',
                ]);
            });
            $grid->column('id')->sortable();
            $grid->column('title')->width('12%')->display(function ($content) {
                $limit = 20; // 设置要显示的字符数
                $ellipsis = '...'; // 设置省略号
                if (mb_strlen($content, 'utf-8') > $limit) {
                    $content = mb_substr($content, 0, $limit, 'utf-8') . $ellipsis;
                }
                return $content;
            });
            $grid->column('content')->width('18%')->display(function ($content) {
                $limit = 50; // 设置要显示的字符数
                $ellipsis = '...'; // 设置省略号
                if (mb_strlen($content, 'utf-8') > $limit) {
                    $content = mb_substr($content, 0, $limit, 'utf-8') . $ellipsis;
                }
                return $content;
            });
            // $grid->column('')->image('http://laraveDact.com/uploads', 100, 100);;
            $grid->column('image')->display(function ($avatar) {
                $domain = Str::startsWith($avatar, 'uploads') ? 'http://wxy.jingyi.icu/' : 'http://laraveDact.com/uploads/';
                return "<img data-action='preview-img' src='{$domain}{$avatar}' width='80' height='80' class='img img-thumbnail'>";
            });
            $grid->column('integral')->width('8%');
            $grid->column('weight')->width('6%');
            $grid->column('attribute');
            $grid->column('created')->width('10%')->sortable();
            $grid->column('updated')->width('10%')->sortable();
        
            // $grid->filter(function (Grid\Filter $filter) {
            //     $filter->equal('id');
            // });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new BoxPointproduct(), function (Show $show) {
            $show->number('id');
            // $show->field('user_id');
            // $show->field('class_id');
            $show->field('title');
            $show->field('content');
            // $show->field('image')->image('http://laraveDact.com/uploads', 100, 100);
            $show->column('image')->display(function ($avatar) {
                $domain = Str::startsWith($avatar, 'uploads') ? 'http://wxy.jingyi.icu/' : 'http://laraveDact.com/uploads/';
                return "<img data-action='preview-img' src='{$domain}{$avatar}' width='80' height='80' class='img img-thumbnail'>";
            });
            $show->field('integral');
            $show->field('weight');
            $show->field('attribute');
            $show->field('created');
            $show->field('updated');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new BoxPointproduct(), function (Form $form) {
            $form->display('id');
            $form->text('title');
            $form->text('weight');
            $form->text('attribute');
            $form->image('image')->disk('public')->accept('jpg,png,gif,jpeg')->maxSize(1024)->required()->autoUpload()->help('大小不要超过1024K');
            $form->number('integral');
            $form->date('created')->format('YYYY-MM-DD')->default(date('Y-m-d'))->required();
            $form->date('updated')->format('YYYY-MM-DD')->default(date('Y-m-d'))->required();
            $form->markdown('content')->options([
                'onpreviewing' => JavaScript::make(
                    <<<JS
            function() {
                // console.log("onpreviewing =>", this, this.id, this.settings);
                // on previewing you can custom css .editormd-preview-active
            }
            JS
                ),
            ])->disk('public')->required();
        });
    }
}
